package com.xying.util;

import com.xying.common.Const;

import org.apache.commons.lang.StringUtils;

import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Properties;

import lombok.extern.slf4j.Slf4j;


/**
 * Created by Hisen on 2017/11/8.
 */
@Slf4j
public class PropertiesUtil {

  private static Properties properties;

  static{
    String fileName = Const.DEFAULT_CONFIG_FILE;
    properties = new Properties();
    try {
      properties.load(new InputStreamReader(PropertiesUtil.class.getClassLoader().getResourceAsStream(fileName)));
    } catch (IOException e) {
      log.error("配置文件读取异常", e);
    }
  }

  public static String getProperty(String key){
    String value = properties.getProperty(key.trim());
    if(StringUtils.isBlank(value)) {
      return null;
    }
    return value.trim();
  }

  public static String getProperty(String key, String defaultValue){
    String value = properties.getProperty(key.trim());
    if(StringUtils.isBlank(value)) {
      value = defaultValue;
    }
    return value.trim();
  }

  public static int getIntProperty(String key){
    return Integer.parseInt(getProperty(key));
  }

  public static int getIntProperty(String key, String defaultValue){
    return Integer.parseInt(getProperty(key, defaultValue));
  }

  public static boolean getBooleanProperty(String key){
    return Boolean.parseBoolean(getProperty(key));
  }

  public static boolean getBooleanProperty(String key, String defaultValue){
    return Boolean.parseBoolean(getProperty(key, defaultValue));
  }
}
